/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.custom

import android.content.Context
import android.net.wifi.SoftApConfiguration
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.TcamHotspotController
import com.ecarx.systemui.plugin.ui.dialog.InputTextDialog
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager
import com.ecarx.systemui.plugin.utils.AppUtils
import com.ecarx.systemui.plugin.utils.LogUtils
import java.lang.Exception

class HotspotOptView : FrameLayout, View.OnClickListener {

    companion object {
        private const val TAG = "HotspotOptView"
    }

    private var mTcamHotspotController:TcamHotspotController? = null
    private var mTvName:TextView? = null
    private var mTvPwd:TextView? = null
    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(
        context!!, attrs, 0
    ) {}

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!, attrs, defStyleAttr
    ) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.layout_popup_hotspot, this, true)
        try {
            mTcamHotspotController = TcamHotspotController()
            mTcamHotspotController?.init(context)
        } catch (exception: ClassNotFoundException) {
            exception.printStackTrace()
        } catch (throwable: NoClassDefFoundError) {
            throwable.printStackTrace()
        } catch (e: Exception){
            e.printStackTrace()
        }

        initView()
    }

    private fun initView() {
        val btSwitch = findViewById<CustomSwitch>(R.id.bt_switch)
        val groupInfos = findViewById<View>(R.id.group_infos)
        // 仅仅 OnClick 时触发
        btSwitch.setSwitchChangeListener(object : CustomSwitch.SwitchChangeListener {
            override fun toClose() {
                groupInfos.visibility = View.GONE
                mTcamHotspotController?.stopHotspot()
            }

            override fun toOpen() {
                groupInfos.visibility = View.VISIBLE
                mTcamHotspotController?.startHotspot()
            }

        })
        if (mTcamHotspotController?.isEnabled() == true) {
            btSwitch.uiOpen()
            groupInfos.visibility = View.VISIBLE
            mTcamHotspotController?.stopHotspot()
        } else {
            btSwitch.uiClose()
            groupInfos.visibility = View.GONE
            mTcamHotspotController?.startHotspot()
        }

        mTvName = findViewById<TextView>(R.id.tv_name)
        mTvPwd = findViewById<TextView>(R.id.tv_pwd)
        mTvName?.setOnClickListener(this)
        mTvPwd?.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_preferences).setOnClickListener(this)

        mTcamHotspotController?.registerHotspotState(object : TcamHotspotController.TcamHotspotState {
            override fun onStateChanged(enable: Boolean) {
                Log.d(TAG, "onStateChanged() called with: enable = $enable")
                if (enable) {
                    btSwitch.uiOpen()
                } else {
                    btSwitch.uiClose()
                }
                updateHotspotInfo()
            }

            override fun onConfigurationChanged(config: SoftApConfiguration) {
                updateHotspotInfo()
            }
        })

        updateHotspotInfo()
    }

    private fun updateHotspotInfo() {
        val nameAndPwd = mTcamHotspotController?.getNameAndPwd()
        mTvName?.text = nameAndPwd?.first
        mTvPwd?.text = nameAndPwd?.second
        Log.d(TAG, "updateHotspotInfo() " + nameAndPwd.toString())
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_name -> {
                DialogManager.showInputDialog(context, false, object : InputTextDialog.InputTextListener {
                    override fun onInput(text: String) {
                        LogUtils.d(TAG, "onInput: $text")
                        mTcamHotspotController?.setHotSpotName(text)
                    }
                })
            }
            R.id.tv_pwd -> {
                DialogManager.showInputDialog(context, true, object : InputTextDialog.InputTextListener {
                    override fun onInput(text: String) {
                        LogUtils.d(TAG, "onInput: $text")
                        mTcamHotspotController?.setHotSpotPwd(text)
                    }
                })
            }
            R.id.tv_preferences -> {
                AppUtils.openHotspotSettings(context)
            }
        }
    }
}